<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery1">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="门店">
              <a-cascader
                :options="departModel.departTreeOptions"
                :showSearch="true"
                expand-trigger="hover"
                placeholder=""
                :fieldNames="departModel.fieldNames"
                style="width: 210px"
                v-model="queryParam.selecteddeparts"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="交易时间" :required="true">
              <a-range-picker
                :ranges="{ 今天: [moment(), moment()], 近7天:[moment().subtract(7, 'days'),moment()],本月: [moment().startOf('month'), moment().endOf('month')] }"
                v-model="orderDateRange"
                @change="handleChange"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="收银单号">
              <a-input v-model="queryParam.orderNo" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <!--收银状态（0待支付 1支付成功 2支付失败 3支付撤销  ）-->
            <a-form-item label="交易状态">
              <a-select allowClear v-model="queryParam.payTradeState">
                <a-select-option value="SUCCESS">支付成功</a-select-option>
                <a-select-option value="USERPAYING">用户支付中</a-select-option>
                <a-select-option value="REVOKED">已撤销</a-select-option>
                <a-select-option value="CLOSED">已关闭</a-select-option>
                <a-select-option value="REFUND">退款成功</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="收款方式">
              <a-select v-model="queryParam.paySource" allowClear>
                <a-select-option value="000">现金</a-select-option>
                <a-select-option value="010">微信</a-select-option>
                <a-select-option value="020">支付宝</a-select-option>
                <a-select-option value="110">银联及其他在线付款</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="支付第三方单号">
              <a-input v-model="queryParam.payThirdNo" allowClear></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery1" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset1" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
        </span>
        <span slot="payTradeState" slot-scope="text, record">
          <!-- （SUCCESS支付成功，REFUND转入退款，NOTPAY未支付，CLOSED已关闭，USERPAYING用户支付中，REVOKED已撤销，NOPAY未支付支付超时，PAYERROR支付失败）退款订单状态（SUCCESS退款成功，FAIL退款失败，REFUNDING退款中，NOREFUND退款超时） -->
          <a-tag color="blue" v-if="text === 'SUCCESS'">支付成功</a-tag>
          <a-tag color="blue" v-if="text === 'REFUND'">转入退款</a-tag>
          <a-tag color="blue" v-if="text === 'NOTPAY'">未支付</a-tag>
          <a-tag color="blue" v-if="text === 'CLOSED'">已关闭</a-tag>
          <a-tag color="blue" v-if="text === 'USERPAYING'">用户支付中</a-tag>
          <a-tag color="blue" v-if="text === 'REVOKED'">已撤销</a-tag>
          <a-tag color="blue" v-if="text === 'NOPAY'">未支付|支付超时</a-tag>
          <a-tag color="blue" v-if="text === 'PAYERROR'">未支付|支付超时</a-tag>
          <a-tag color="blue" v-if="text === 'NOPAY'">支付失败</a-tag>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { queryDepartTreeList1 } from '@/api/api'
import moment from 'moment'

export default {
  name: 'PosPayRecordList',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      description: '交易流水管理页面',
      disableMixinCreated: true,
      moment,
      orderDateRange: [],
      paySourceDict: {
        '000': '现金',
        '010': '微信',
        '020': '支付宝',
        110: '银联',
      },
      departModel: {
        departTreeOptions: [],
        fieldNames: { label: 'title', value: 'value', children: 'children' },
      },
      // 表头
      columns: [
        {
          title: '所属机构',
          align: 'center',
          dataIndex: 'orgName',
        },
        {
          title: '下单门店',
          align: 'center',
          dataIndex: 'shopName',
        },
        {
          title: '收银订单号',
          align: 'center',
          dataIndex: 'orderNo',
        },
        {
          title: '收款方式',
          align: 'center',
          dataIndex: 'payType',
          customRender: (text, record) => {
            if (record.payType === 1) {
              return '现金'
            }
            if (record.paySource === '010') {
              return '微信'
            }
            if (record.paySource === '020') {
              return '支付宝'
            }
            return '银联及其他在线付款'
          },
        },
        {
          title: '请求交易时间',
          align: 'center',
          dataIndex: 'requestTime',
        },
        {
          title: '交易成功时间',
          align: 'center',
          dataIndex: 'paySuccTime',
        },
        {
          title: '在线支付第三方单号',
          align: 'center',
          dataIndex: 'payThirdNo',
          customRender: (text, record) => {
            return text ? text : '-'
          },
        },
        {
          title: '付款金额(元)',
          align: 'center',
          dataIndex: 'payAmount',
        },
        {
          title: '交易状态',
          align: 'center',
          dataIndex: 'payTradeState',
          scopedSlots: { customRender: 'payTradeState' },
        },
      ],
      url: {
        list: '/cashier/posPayRecord/list',
      },
    }
  },
  computed: {},
  created() {
    this.loadDepartTree()
    this.init()
    this.searchQuery1()  
  },
  methods: {
    init(){
      
      this.orderDateRange = [moment().subtract(7, 'days'),moment()]
      this.queryParam.beginTime = moment().subtract(7, 'days').format('YYYY-MM-DD')
      this.queryParam.endTime = moment().format('YYYY-MM-DD')
    },
    handleChange(date, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },
    assembleQueryParam() {
      console.log(this.queryParam.selecteddeparts)
      let selecteddeparts = this.queryParam.selecteddeparts
      if (selecteddeparts && selecteddeparts.length == 2) {
        this.queryParam.orgIds = selecteddeparts[0]
        this.queryParam.shopIds = selecteddeparts[1] == 0 ? null : selecteddeparts[1]
      } else {
        this.queryParam.orgIds = null
        this.queryParam.shopIds = null
      }

      

    },
    searchReset1(){
      this.queryParam = {
        beginTime: this.queryParam.beginTime,
        endTime: this.queryParam.endTime,
      }
      this.searchQuery1()
    },
    searchQuery1(){
      if(!this.queryParam.beginTime){
        this.$message.warning('请选择时间')
        return false
      }

      this.assembleQueryParam()
      this.loadData()
    },
    loadDepartTree() {
      queryDepartTreeList1().then((res) => {
        if (res.success) {
          let departList = res.result
          if (departList.length == 1 && departList[0].orgType == 1) {
            departList = departList[0].children
          }
          this.departModel.departTreeOptions = departList

          this.departModel.departTreeOptions.forEach((x) => {
            if (!x['children']) {
              x['children'] = []
            }
            let allShop = { title: '全部门店', value: '0' }
            x['children'].unshift(allShop)
          })
        }
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>